<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div {
      width: 100px;
      height: 100px;
      background-color: black;
      position: absolute;
      top: 0px;
      left: 0px;
    }
  </style>
</head>

<body>
  <div></div>
  <script>
    var div = document.querySelector('div');
    //给div注册监听事件
    div.addEventListener('mousedown', function () {
      //给页面注册监听事件
      document.addEventListener('mousemove', move);
      //当我鼠标松开时注册监听事件
      document.addEventListener('mouseup', function () {
        // 移出页面事件
        document.removeEventListener('mousemove', move)
      })
    })

    function move(e) {
      var x = e.pageX;
      var y = e.pageY;
      div.style.left = x - 50 + 'px'
      div.style.top = y - 50 + 'px'
    }
  </script>
</body>

</html>